package com.gr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.entity.Attr;
import com.gr.entity.ResultEntity;
import com.gr.service.IAttrService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author guoran
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/attr")
public class AttrController {
    @Autowired
    private IAttrService attrService;
    @GetMapping("/selectAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectAttrByGroupId(@PathVariable("groupId") Integer groupId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("attr_group_id",groupId);
        List<Attr> list = attrService.list(wrapper);
        return ResultEntity.ok(list);
    }

    @GetMapping("/selectCommonAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectCommonAttrByGroupId(@PathVariable("groupId")Integer groupId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("attr_type",groupId);
        List<Attr> list = attrService.list(wrapper);
        return ResultEntity.ok(list);
    }

    @PostMapping("/addCommonAttr")
    public ResultEntity addCommonAttr(@RequestBody Attr attr){
        attrService.save(attr);
        return ResultEntity.ok();
    }

    @DeleteMapping("/delete/{attrId}")
    public ResultEntity delete(@PathVariable("attrId")Integer attrId){
        attrService.removeById(attrId);
        return ResultEntity.ok();
    }

    @GetMapping("/selectOne/{attrId}")
    public ResultEntity<Attr> selectOne(@PathVariable("attrId")Integer attrId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("attr_id",attrId);
        Attr one = attrService.getOne(wrapper);
        return ResultEntity.ok(one);
    }

    @PostMapping("/update")
    public ResultEntity update(@RequestBody Attr attr){
        attrService.updateById(attr);
        return ResultEntity.ok();
    }

    @GetMapping("/selectSaleAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectSaleAttrByGroupId(@PathVariable("groupId")Integer groupId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("attr_type",groupId);
        List<Attr> list = attrService.list(wrapper);
        return ResultEntity.ok(list);
    }

    @PostMapping("/addSaleAttr")
    public ResultEntity addSaleAttr(@RequestBody Attr attr){
        attrService.updateById(attr);
        return ResultEntity.ok();
    }
}

